﻿using AutoMapper;
using BaseData.CommonLib.Models;
using BaseData.DomainLib.AggregatesModel.WarehouseAggregate;
using MediatR;
using Swimj.CommonLib.Base;

namespace BaseData.ApplicationLib.Commands
{
    public class UpdateWarehouseCommand: WarehouseEditRequestDto, IRequest<Result<WarehouseInfoResponseDto>>
    {
        public int Id { get; set; }
    }

    public class UpdateWarehouseCommandHandler : IRequestHandler<UpdateWarehouseCommand, Result<WarehouseInfoResponseDto>>
    {
        private readonly IWarehouseRepository _processRepository;
        private readonly IMapper _mapper;

        public UpdateWarehouseCommandHandler(IWarehouseRepository processRepository,
            IMapper mapper)
        {
            _processRepository = processRepository;
            _mapper = mapper;
        }

        public async Task<Result<WarehouseInfoResponseDto>> Handle(UpdateWarehouseCommand request, CancellationToken cancellationToken)
        {
            var process = await _processRepository.Get(request.Id);
            if (process != null)
            {
                process.Update(request.Code, request.Name,request.WarehouseType, request.Address,
                    request.Description);
            }

            await _processRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

            return await Result<WarehouseInfoResponseDto>.SuccessAsync(_mapper.Map<WarehouseInfoResponseDto>(process));
        }
    }
}